import React from "react";
import {View} from "react-native";
import InputForm from "../components/form/InputForm";
import {globalStore} from "../libs/ShareStore";
import {SelectFormData} from "../components/form/SelectForm";
import {pageStyle} from "./style/pageStyle";
import EButton from "../components/utils/EButton";
import EHr from "../components/utils/EHr";
import AvatarHeader from "../components/utils/AvatarHeader";
import ANavigationComponent, {ANavigationComponentProp} from "../components/navigator/ANavigationComponent";
import RoomGetDTO from "../libs/sdk/echatim/model/dto/RoomGetDTO";
import Beans from "../libs/sdk/echatim/common/utils";
import {FileInfo} from "../libs/sdk/echatim/core/fileclient/FileServerClient";
import EChatIMApis from "../libs/sdk/echatim/core/apis/EChatIMApis";


type ModifyRoomInfoPageProp = {
}& ANavigationComponentProp

type ModifyRoomInfoPageState = {
    avatar:string,
    name:string,
    announce:string,
}

export default class ModifyRoomInfoPage extends ANavigationComponent<ModifyRoomInfoPageProp, ModifyRoomInfoPageState>  {
    constructor(props:ModifyRoomInfoPageProp) {
        super(props);
        const roomInfo = this.getNavigationParam('info') as RoomGetDTO;
        this.state = {
            avatar:roomInfo.avatar,
            name:roomInfo.name,
            announce:roomInfo.announce
        };
        globalStore.chatsContent.roomInfo = Beans.replace(globalStore.chatsContent.roomInfo, roomInfo);
    }

    exitRoom(){
        // TODO: exitRoom
        globalStore.showToast("操作成功");
    }


    onModifyRoomInfo(){
        globalStore.onModifyRoomInfoPressOk();
    }

    onAvatarUpload(file:FileInfo){
        globalStore.chatsContent.roomInfo.avatar = file.url;
        EChatIMApis.updateRoom.call(globalStore.chatsContent.roomInfo).then((resp) => {
            globalStore.showToast("修改头像成功")
        }).catch((e) => {
            globalStore.showApiErrorToast(e);
        })
    }

    render() {
        // 群加入模式
        const confJoinmodeEnum:Array<SelectFormData> = [];
        // 群员被邀请方式
        const confBeinviteEnum:Array<SelectFormData> = [];
        // 群员邀请权限
        const confInviteotherEnum:Array<SelectFormData> = [
            {value:'OWNER', label:'仅群主'},
            {value:'ALL',label:'任何人也可以'},
        ];
        // 群信息更新权限
        const confUpdateEnum:Array<SelectFormData> = [
            {value:'OWNER', label:'仅群主'},
            {value:'ALL',label:'任何人也可以'},
        ];


        return (
            <View style={pageStyle.container}>
                <AvatarHeader avatar={this.state.avatar}
                              headerFirstText={this.state.name}
                              headerSecondText={this.state.announce}
                              showEditAvatar={true}
                              onAvatarUpload={(info)=>this.onAvatarUpload(info)}/>
                <InputForm startText={'群名:'} defaultValue={globalStore.chatsContent.roomInfo.name} onChangeInput={(text)=>{globalStore.chatsContent.roomInfo.name = text}} />
                <InputForm startText={'群公告:'} defaultValue={globalStore.chatsContent.roomInfo.announce} onChangeInput={(text)=>{globalStore.chatsContent.roomInfo.announce = text}} />
                <InputForm startText={'群简介:'}  defaultValue={globalStore.chatsContent.roomInfo.introduce} onChangeInput={(text)=>{globalStore.chatsContent.roomInfo.introduce = text}} />
                {/*<SelectForm startText={'加入模式:'} optionValue={confJoinmodeEnum}  defaultValue={globalStore.chatsContent.roomInfo.confJoinmode} onChangeSelect={(value, index)=>{globalStore.chatsContent.roomInfo.confJoinmode = value}} />*/}
                {/*<SelectForm startText={'被邀请方式:'} optionValue={confBeinviteEnum}  defaultValue={globalStore.chatsContent.roomInfo.confBeinvite} onChangeSelect={(value, index)=>{globalStore.chatsContent.roomInfo.confBeinvite = value}} />*/}
                {/*<SelectForm startText={'邀请权限:'} optionValue={confInviteotherEnum}  defaultValue={globalStore.chatsContent.roomInfo.confInviteother} onChangeSelect={(value, index)=>{globalStore.chatsContent.roomInfo.confInviteother = value}} />*/}
                {/*<SelectForm startText={'信息更新:'} optionValue={confUpdateEnum}  defaultValue={globalStore.chatsContent.roomInfo.confUpdate} onChangeSelect={(value, index)=>{globalStore.chatsContent.roomInfo.confUpdate = value}} />*/}
                <InputForm startText={'最大成员数:'}  defaultValue={globalStore.chatsContent.roomInfo.maxMember+''} onChangeInput={(text)=>{globalStore.chatsContent.roomInfo.maxMember=parseInt(text)}} />
                <EButton title={'修改讨论组信息'} onPress={()=>{this.onModifyRoomInfo()}}/>
                <EHr height={10} />
                <EButton title={'退出讨论组'} onPress={()=>{this.exitRoom()}}/>
            </View>
        )
     }
}
